<script>
import { getCollection } from '@/api/api'
export default {
    data() {
        return {
            len: 0,
            changeType: 1,
            datalist: [],
            options: [{
                value: '10197',
                label: '省政企'
            }, {
                value: '30182',
                label: '哈尔滨市‌'
            }, {
                value: '30183',
                label: '齐齐哈尔市'
            }, {
                value: '30184',
                label: '牡丹江市'
            }, {
                value: '30185',
                label: '佳木斯市'
            }, {
                value: '30186',
                label: '大庆市'
            }, {
                value: '30187',
                label: '伊春市‌'
            }, {
                value: '30188',
                label: '鸡西市‌'
            }, {
                value: '30189',
                label: '鹤岗市‌'
            }, {
                value: '30190',
                label: '双鸭山市'
            }, {
                value: '30191',
                label: '‌七台河市‌'
            }, {
                value: '30192',
                label: '绥化市‌'
            }, {
                value: '30193',
                label: '黑河市‌'
            }, {
                value: '30194',
                label: '大兴安岭'
            }],
        }
    },
    mounted() {
        this.changeTable()
    },
    methods: {
        changeTable() {
            getCollection().then(res => {
                res.map(item => {
                    item.pre = this.getPreNum(item);
                })
                this.datalist = res ? this.getSort(res) : [];
                this.len = this.datalist.length - 3 || 0
            })
        },
        getSort(array) {
            const sortedArray = array.sort((a, b) => {
                return b.pre - a.pre
            });
            return sortedArray
        },
        getPreNum(item) {
            let num = item.monthActualAmount / item.monthPlanAmount;
            return this.getPre(num);

        },
        getPre(num) {
            return num ? this.truncateToFixed(num * 100, 1) : 0
        },
        getNum(num) {
            return num ? this.numberFormatter(num, 2) : 0;
        },
        truncateToFixed(value, decimals) {
            const integerPart = Math.trunc(value); // 获取整数部分
            const decimalPart = value - integerPart; // 获取小数部分
            const factor = Math.pow(10, decimals);
            const truncatedDecimalPart = decimalPart < 0 ? Math.ceil(decimalPart * factor) / factor : Math.floor(decimalPart * factor) / factor; // 根据正负选择向上或向下截断
            return integerPart + truncatedDecimalPart; // 组合整数部分和小数部分
        },
        goLink(cityName) {
            let find = this.options.find(item => item.label == cityName);
            this.$router.push({ path: '/projectTable', query: { cityCode: find.value || '', type: 'totalIncome' } });
        },
        numberFormatter(num, digits) {
            // const si = [
            //     // { value: 1E8, symbol: '亿元' },
            //     { value: 1E4, symbol: '万元' }
            // ]
            // for (let i = 0; i < si.length; i++) {
                // if (num >= 0) {
                //     if (num >= si[i].value) {
                //         return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1')
                //     }
                // } else {
                //     if (Math.abs(num) >= si[i].value) {
                //         return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1')
                //     }
                // }
            // }
            return (num / 1E4).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1').toString()
        },
    }
}
</script>

<template>
    <div class="table-wrap">
        <div class="table-head-wrap">
            <el-row>
                <el-col :span="1" class="el-style">
                    <div class="head-content">
                        <img :src="require('@/assets/img/icon6.png')" width="30" height="30" class="icon" />
                    </div>
                </el-col>
                <el-col :span="3" class="el-style">
                    <div class="head-content">地市</div>
                </el-col>
                <el-col :span="4" class="el-style">
                    <div class="head-content">有出账计划项目数</div>
                </el-col>
                <el-col :span="5" class="el-style">
                    <div class="head-content">上月计划出账总金额(万元)</div>
                </el-col>
                <el-col :span="5" class="el-style">
                    <div class="head-content">上月实际出账总额(万元)</div>
                </el-col>
                <el-col :span="6" class="el-style">
                    <div class="head-content">上月计划转收率</div>
                </el-col>
            </el-row>
        </div>
        <div class="table-body-wrap">
            <el-row v-for="(item, i) in datalist" :key="i" class="tr-bg">
                <el-col :span="1" class="el-style">
                    <div class="body-content">
                        <span class="num"
                            :style="{ backgroundColor: i == 0 ? '#ff3e33' : i == 1 ? '#ffa944' : i == 2 ? '#0ae7be' : '#0b82ff' }">{{
                                i + 1 }}</span>
                    </div>
                </el-col>
                <el-col :span="3" class="el-style">
                    <div class="body-content">{{ item.cityName || '--' }}</div>
                </el-col>
                <el-col :span="4" class="el-style">
                    <div class="body-content">{{ item.planProjectCount || 0 }}</div>
                </el-col>
                <el-col :span="5" class="el-style">
                    <div class="body-content">{{ getNum(item.monthPlanAmount) }}</div>
                </el-col>
                <el-col :span="5" class="el-style">
                    <div class="body-content">{{ getNum(item.monthActualAmount) }}</div>
                </el-col>
                <el-col :span="6" class="el-style">
                    <div class="body-content">
                        <span :class='["border-left", (2 < i && i < len) && "blue-left", i >= len && "yellow-left"]'></span>
                        <img :src="require(i < 3 ? '@/assets/img/green.png' : (2 < i && i < len) ? '@/assets/img/blue.png' : '@/assets/img/yellow.png')"
                            width="19" height="19" class="icon-pie" />
                        <span class="pie-value">{{ item.pre }}%</span>
                        <span
                            :class='["border-right", (2 < i && i < len) && "blue-right", i >= len && "yellow-right"]'></span>
                    </div>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<style scoped lang="scss">
.table-wrap {
    width: 100%;
    height: 520px;
    margin-top: 16px;
    box-sizing: border-box;
    font-size: 16px;

    .table-head-wrap {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 50px;

        .el-style {
            .head-content {
                position: relative;
                overflow: hidden;
                width: 100%;
                height: 50px;
                line-height: 50px;
                text-align: center;
                font-weight: bolder;
                color: #0b82ff;
                font-size: 15.5px;
            }
        }
    }

    .table-body-wrap {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: calc(100% - 40px);
        background: white;
        overflow: auto;
        overflow-x: hidden;
        margin-top: 5px;

        &::-webkit-scrollbar {
            /*滚动条整体样式*/
            width: 0px;
            /*高宽分别对应横竖滚动条的尺寸*/
            height: 6px;
        }

        &::-webkit-scrollbar-thumb {
            /*滚动条里面小方块*/
            border-radius: 10px;
            background: #ececec;
        }

        &::-webkit-scrollbar-track {
            /*滚动条里面轨道*/
            border-radius: 10px;
            background: transparent;
        }

        .el-style {
            .body-content {
                position: relative;
                overflow: hidden;
                width: 100%;
                height: 47px;
                line-height: 47px;
                text-align: center;
            }
        }

        .tr-bg {
            background: #f3fdfd;
        }

        .tr-bg:nth-child(2n) {
            background: #fff;
        }
    }
}

.icon {
    margin-top: 10px;
}

.num {
    color: #fff;
    background: #0b82ff;
    border-radius: 100px;
    width: 20px;
    height: 20px;
    text-align: center;
    line-height: 20px;
    margin: 10px;
    font-size: 14px;
    display: inline-block;
}

.icon-pie {
    position: absolute;
    left: 70px;
    top: 15px;
}

.pie-value {
    margin-left: 16px;
}

.border-left {
    width: 50px;
    height: 16px;
    position: absolute;
    background: #cef8f2;
    left: 16px;
    top: 17px;
    background: linear-gradient(to right, #cef8f2, transparent);
}

.border-right {
    width: 50px;
    height: 16px;
    position: absolute;
    background: #cef8f2;
    right: 20px;
    top: 17px;
    background: linear-gradient(to left, #cef8f2, transparent);
}

.blue-left {
    background: linear-gradient(to right, #c6e5fd, transparent);
}

.yellow-left {
    background: linear-gradient(to right, #ffefdd, transparent);
}

.blue-right {
    background: linear-gradient(to left, #c6e5fd, transparent);
}

.yellow-right {
    background: linear-gradient(to left, #ffefdd, transparent);
}

.cityName {
    cursor: pointer;

    &:hover {
        color: #0b82ff;
    }
}
</style>
